package article_management;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class ArticalProcess extends FileArtical{
	Display is=new Display();
	protected Pagination pg=new Pagination();
	protected Display dis=new Display();
	public static boolean SWrite=false;
	public static boolean SRead=false;
	public static boolean SDelete=false;
	public static boolean SUpdate=false;
	public static boolean SRestore=false;
	public static boolean SFind=false;
	public static int LastID;
	protected InputIO in=new InputIO();
	public ArticalProcess(){
		try {
			LastID=Read_AutoID();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//=======================  Read Process =======================
	public boolean ReadData(){
		int id=0;
		int index=0;
		do{
			if(SRead){ //if used Short cut key
				id=Integer.parseInt(Main.shortcutString.trim());
				
				SRead=false;
			}
			else{      // input 
				id=dis.InputId("Please Input Id that you want to Read : ");
			}
			try{
				index=in.CheckId(id); // Check input id exist or not
				if(index!=-1){  //  return -1 is ! exist
					Artical ar=read(id+"");
					dis.displayReadData(ar); // Show Layout for input information
				}
				else System.out.println("Id not found!!!!");
				in.input();break;
			}catch(Exception e){
				
			}
		}while(true);
		is.ClearScreen(3);
		return false;
	}
	//===================== Write Process ======================
	public boolean WriteData() throws IOException{
		String st[]=null;
		Artical ar;
		do{
			if(SWrite){  // Use Short cut key for Write
				st=Main.shortcutString.split("//");
				SWrite=false;
			}
			else
				st=is.displayInsertData();
			try {
				LastID+=1;
				ar=new Artical(LastID+"",st[0],st[1],getMachineTime(),st[2]);
				write(LastID+"", ar);    //Write Data to file
				write_AutoID(LastID);    // Write new ID or AutoId
				///add new Id to ArrayList at position 0
				Pagination.listNameFile.add(0,LastID); 
				System.out.println("Data write successfully!!!!!");
				if(!is.ContinueOrNot("Do you want to continue ?(y/n)"))break;
		
			} catch(Exception e){
				System.out.println("Error Write Data!!!");
			}
		}while(true);
		is.ClearScreen(4);
		return false;
	}
	
	public boolean DeleteData(){
		int id=0,index=0;
		do{
			if(SDelete){ 
				id=Integer.parseInt(Main.shortcutString.trim());
				SDelete=false;
			}
			else
				id=dis.InputId("Please Input Id that you want to Delete : ");
			try{
				if(id!=-1){
					index=in.CheckId(id); // Check input id exist or not
					if(index!=-1){ // if Id found return !-1
						if(is.ContinueOrNot("Do you want to Delete ?(y/n)")){
							if(Delete(id+"")){ // if delete success return true...
								Pagination.listNameFile.remove(index); // remove id by index from list file
								System.out.println("Delete Success");
							}else{
								System.out.println("Delete Not success please check agin...");
								break;
							}
						}
						else break;
					}
					else System.out.println("Id not found!!!!");
				}
				if(!is.ContinueOrNot("Do you want to continue delete ?(y/n)"))
						break;
					else continue;
			}catch(Exception e){
				System.out.println("Exception on process delete!!");
			}
		}while(true);
		is.ClearScreen(4);
		return false;
	}
	public boolean UpdateData(){
		int id=0,index=0;
		Artical ar;
		do{
			if(SUpdate){ 
				id=Integer.parseInt(Main.shortcutString.trim());
				SUpdate=false;
			}
			else
				id=dis.InputId("Please Input Id that you want to Update : ");
			try{
				if(id!=-2){
					index=in.CheckId(id); // check input id exist or not
					if(index!=-1){ // if Id found return index in list
						do{
							ar=read(id+"");
							dis.showChooseField(); // show layout for choose Artical field
							ar=UpdateField(ar); // get select field from function below
							if(ar!=null){
								Update(id+"", ar);
							}else return false;
							if(!is.ContinueOrNot("Do you want to continue update other field ?(y/n)"))break;
							System.out.println("Update Successfully!!!!!!");
						}while(true);
					}
					else System.out.println("Id not found!!!!");
				}
					if(!is.ContinueOrNot("Do you want to continue Update ?(y/n)"))
					break;
				else continue;
			}catch(Exception e){
				System.out.println("Exception on process update!!");
			}
		}while(true);
		is.ClearScreen(4);
		return false;
	}
	public Artical UpdateField(Artical ar){

		int field=0;
		Artical a_temp=null;
		do{
			try{
				field=in.inputNumber(); // input only number 
				if(field==1 || field==2 || field==3 || field==4){
					String str="";
					switch(field){
						case 1:
							System.out.println("New Title  : ");
							str=in.input(); // update Title
							a_temp=new Artical(ar.getId(), str, ar.getAuthor(),getMachineTime(), ar.getContent());
							break;
						case 2:
							System.out.print("New Author   : ");
							str=in.input(); // update Author
							a_temp=new Artical(ar.getId(),ar.getTitle(),str, getMachineTime(), ar.getContent());
							break;
						case 3:
							System.out.println("		         	    New Content ");
							System.out.println("--------------------------------------------------------------------------");
							System.out.print("  ");str=in.inputMultiLine();
							a_temp=new Artical(ar.getId(),ar.getTitle(),ar.getAuthor(), getMachineTime(),str);
							break;
						case 4: return null;
					}
					return a_temp;
				}
				else{ // choose again when you input invalid data
					System.out.print("Choose: ");
				}
				
			}catch(Exception e){
				System.out.println("Exception on choose update field");
			}
		}while(true);
	}
	public boolean SearchData(){
		String str="";
		if(SFind){ // search key short cut exist...
			str=Main.shortcutString.trim();
			SFind=false;
		}
		else{   // search key not use short cut
			System.out.print("Input key that you want to search : ");
			str=in.input();
		}
		PaginationProcess.listNameFile=Search(str); 
		Pagination.PageShow=5;
		Pagination.PStart=0;
		Pagination.TPage=Pagination.listNameFile.size();
		Display.Lsearch=false;
		Display.SLRestore=true;
		return false;
	}
	public boolean RestoreData(){
		if(!SRestore){  
			if(dis.ContinueOrNot("Do you want to Restore file ?(y/n)")){
				FileArtical.FolderContent="Restore"; // change static content to restore
				Pagination.listNameFile=FileArtical.ListRestore(); // load data from Restore folder
				Display.SRestore=true;
				SRestore=true;
				Pagination.PageShow=5;
				Pagination.PStart=0;
				Pagination.TPage=Pagination.listNameFile.size();
			}
		}
		else{
			Restore();
		}
		return false;
	}
	public boolean Restore(){
		int id=0,index;
		do{
			System.out.println("Input id that you want to restore : ");
			id=in.inputNumber();
			if(id!=-2){  // input invalid data 
				index=in.CheckId(id);
				if(index!=-1){   // check input id exist in file Restore or not
					try {      // index !=-1 is meas id exist
						copyFile("Restore","Content", id+""); //copy from restore folder to content folder 
						Delete(id+"");  // delete file by id
						Pagination.listNameFile.remove(index);// remove in list show
						System.out.println("Restore successfully");
					} catch (IOException e) {
						System.out.println("Data restore error !!!!");
					}
				}else{
					System.out.println("Id not found.....");
				}
			}
			if(!is.ContinueOrNot("Do you want to continue Restore ? (y/n)"))
				break;
			else continue;
		}while(true);
		return false;
	}
	public String getMachineTime(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		Calendar cal = Calendar.getInstance();
		return dateFormat.format(cal.getTime());
	}
}
